package com.shop.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shop.constant.DiscountConstant;
import com.shop.constant.MessageConstant;
import com.shop.constant.StatusConstant;
import com.shop.dto.DiscountDTO;
import com.shop.dto.DiscountPageQueryDTO;
import com.shop.entity.Discount;
import com.shop.exception.DeletionNotAllowedException;
import com.shop.mapper.DiscountMapper;
import com.shop.result.PageResult;
import com.shop.vo.DiscountVO;
import com.shop.service.DiscountService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

@Service
public class DiscountServiceImpl implements DiscountService {
    @Autowired
    private DiscountMapper discountMapper;

    /**
     * 商品分页查询
     *
     * @param discountPageQueryDTO 分页查询参数对象
     * @return 分页结果
     */
    @Override
    public PageResult pageQuery(DiscountPageQueryDTO discountPageQueryDTO) {
        PageHelper.startPage(discountPageQueryDTO.getPage(), discountPageQueryDTO.getPageSize());
        Page<DiscountVO> page = discountMapper.pageQuery(discountPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 商品批量删除
     *
     * @param ids 待删除商品的id列表
     */
    @Override
    @Transactional
    public void deleteBatch(List<Long> ids) {
        // 判断当前商品是否能够删除---是否存在起售中的商品？
        for (Long id : ids) {
            Discount discount = discountMapper.getById(id);
            if (discount.getStatus() == StatusConstant.ENABLE) {
                // 当前商品处于起售中，不能删除
                throw new DeletionNotAllowedException(MessageConstant.Discount_ON_SALE);
            }
        }
        // 删除商品表中的商品数据
        for (Long id : ids) {
            discountMapper.deleteById(id);
        }
    }

    /**
     * 商品起售停售
     *
     * @param status 状态（起售或停售状态值）
     * @param id 商品id
     */
    @Override
    @Transactional
    public void startOrStop(Integer status, Long id) {
        Discount discount = Discount.builder()
                .id(id)
                .status(status)
                .build();
        discountMapper.update(discount);
    }

    /**
     * 根据分类id查询商品
     *
     * @param discountType 分类id
     * @return 商品列表
     */
    @Override
    public List<Discount> list(Long discountType) {
//        Discount discount = Discount.builder()
//                .discountType(DiscountConstant.HUODONG)
//                .build();
        return discountMapper.list(discountType);
    }

    /**
     * 新增商品
     *
     * @param discountDTO 商品传输对象
     */
    public void save(DiscountDTO discountDTO) {
        // 检查product_id是否为null
        if (discountDTO.getProductId() == null) {
            throw new IllegalArgumentException("product_id不能为null");
        }
        // 处理endTime的默认值
        if (discountDTO.getEndTime() == null) {
            // 设置为未来100年的时间（可按需调整）
            discountDTO.setEndTime(LocalDateTime.now().plus(100, ChronoUnit.YEARS));
        }
        Discount discount = new Discount();
        BeanUtils.copyProperties(discountDTO, discount);
        discountMapper.insert(discount);

    // 其他方法...
}

    /**
     * 根据id更新商品信息
     *
     * @param discountDTO 包含更新信息的商品传输对象
     */
    @Override
    @Transactional
    public void update(DiscountDTO discountDTO) {
        Discount discount = discountMapper.getById(discountDTO.getId());
        if (discount != null) {
            BeanUtils.copyProperties(discountDTO, discount);
            discountMapper.update(discount);
        }
    }

    /**
     * 根据id查询商品信息
     *
     * @param id 商品id
     * @return 商品实体
     */
    @Override
    public Discount getById(Long id) {
        return discountMapper.getById(id);
    }
}